////////////////////////////////////////////////////////////////////////////////
/// TimerBlink50Hz.c
/// Example File to blink all LEDS attached to PORTB at 50hz using TIMER1
/// Connections:
/// Leds connected to PORTB0-7 Active Low
/// Clock set at 1mHz
////////////////////////////////////////////////////////////////////////////////


/// NO PRESCALER 
////////////////////////////////////////
/// Timer Resolution = (1 / Input Frequency)
////////////////////////////////////////
/// NO PRESCALER
////////////////////////////////////////
/// Target Count = (1 / Target Frequency) / ( 1 / Timer Clock Frequency) - 1
/// = (1 / 20) / (1 / 1000000) - 1
/// = 49999
////////////////////////////////////////
/// PRESCALER ENABLED
////////////////////////////////////////
/// Timer Resolution = (Prescale / Input Frequency)
////////////////////////////////////////
///Prescaler Value | Resolution @ 1MHz 
///              1 |  1uS 
///              8 |  8uS 
///             64 | 64uS 
///            256 | 256uS 
///           1024 | 1024uS
////////////////////////////////////////
/// Target Timer Count = (Input Frequency / Prescale) / Target Frequency - 1
////////////////////////////////////////




//Include the Header file for Input/Output Operations
#include <avr/io.h>


int main(void) { 
	//////////////////////
	/// Initialization ///
	//////////////////////
	DDRB |= 255; //Set PORTB for Output (using bit-OR statement)

	//Timer control register
	TCCR1B |= ((1 << CS00) | (1 << CS01)) ; // Set up timer for fOSC/64 prescaler
	//TCCR1B |= (1 << CS00); //Set up timer for no prescaler
	
	////////////
	/// Loop ///
	////////////
	for (;;) { //Infinite Loop
		
		if (TCNT1 >= 15624) { //Check for 1S
      	//if (TCNT1 >= 49999)	{ //Check for 1/20 Sec

			PORTB ^= 255; //Toggle LED

			TCNT1 = 0; //Reset the timer value
		
		}
	}
}
